package com.solocredit.finance.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import com.solocredit.finance.R
import com.solocredit.finance.databinding.DialogMullaVersionBinding
import com.solocredit.finance.databinding.DialogMullaVersionItemBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：提示dialog
 */
class VersionUpdateDialog: ProDialog {

    private lateinit var binding: DialogMullaVersionBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaVersionBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        val releaseData = MullaCustomConfigsHelper.getMullaVersionObject()
        releaseData?.let {
            binding.textVersionName.text = "V${it.optString("versionName")}"
            //添加更新描述
            setKeurAppUpdateReason(it.optString("versionRemark"))

            val showKey = "30s"
            val showText = String.format(context.getString(R.string.dialog_version_within),showKey)
            val index = showText.indexOf(showKey)
            val stringSpannable = SpannableString(showText)
            stringSpannable.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF2E00")), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            //加粗
            stringSpannable.setSpan(StyleSpan(Typeface.BOLD), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            binding.textUpdateWithin.text = stringSpannable

            //是否强制更新
            val isMustUpdate = MullaCustomConfigsHelper.isMullaVersionUpdateMust()
            if (isMustUpdate){
                binding.imgClose.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.imgClose.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun initListener() {
        setOnClickListener(binding.imgClose) //关闭
        setOnClickListener(binding.btnUpdateNow) //立即更新
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.imgClose -> {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_VERSION_UPDATE_CLOSE,eventRemark)
            }
            R.id.btnUpdateNow -> {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_VERSION_UPDATE_NOW,eventRemark)
                mCallback?.onUpdateClick()
            }
        }
        dismiss()
    }

    /**
     * 添加更新描述
     */
    private fun setKeurAppUpdateReason(remark: String){
        binding.updateReasonLayout.removeAllViews()
        if (!remark.contains("$")){
            val itemBinding = DialogMullaVersionItemBinding.inflate(layoutInflater)
            itemBinding.textUpdateReason.text = remark
            binding.updateReasonLayout.addView(itemBinding.root)
            return
        }
        val list = remark.split("$")
        list.forEach {
            val itemBinding = DialogMullaVersionItemBinding.inflate(layoutInflater)
            itemBinding.textUpdateReason.text = it
            binding.updateReasonLayout.addView(itemBinding.root)
        }
    }

    interface IDialogCallback {
        //立即更新
        fun onUpdateClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_VERSION_UPDATE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addMullaEventAction(MullaEventType.DIALOG.MULLA_DIALOG_VERSION_UPDATE_DISMISS,eventRemark)
    }
}